<template>
  <div id="transResult">
    <Splitpanes horizontal>
      <Pane :size="50">
        <vxe-toolbar perfect>
          <template #buttons>
            <div class="smallToolTitle">文字结果</div>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="iloading" @click="findTextResult"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-square-plus" content="新增" @click="handleAdd"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-save" content="保存" @click="handleSave"></vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="handleDelete"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-sort" content="数据同步" :loading="showDataLoading" @click="pullTextResult"></vxe-button>
          </template>
        </vxe-toolbar>
        <div class="tableBox">
          <vxe-table show-overflow keep-source ref="transResultRef" height="auto" :header-cell-style="{ height: '28px', padding: '0px 2px' }" :column-config="{ resizable: true }" :loading="loading" :data="textResultList" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: false, showIcon: false }">
            <vxe-column type="checkbox" width="30" fixed="left" align="left"></vxe-column>
            <!--            <vxe-column title="序号" field="seq" width="40" :edit-render="{ autofocus: '.vxe-input&#45;&#45;inner' }">-->
            <!--              <template #edit="{ row }">-->
            <!--                <vxe-input v-model="row.seq" type="number"></vxe-input>-->
            <!--              </template>-->
            <!--            </vxe-column>-->
            <vxe-column title="文字结果" field="result" align="left" :edit-render="{ autofocus: '.vxe-input--inner' }">
              <template #edit="{ row }">
                <vxe-input v-model="row.result" type="text" @dblclick="dblclickresult(row)"></vxe-input>
              </template>
            </vxe-column>
            <vxe-column title="结果标志" field="resultSign" align="left" :edit-render="{ autofocus: '.vxe-input--inner' }">
              <template #default="{ row }">
                <span>{{ format1(row.resultSign) }}</span>
              </template>
              <template #edit="{ row }">
                <vxe-select v-model="row.resultSign" placeholder="请选择结果标志" clearable transfer>
                  <vxe-option v-for="resultSign in resultSigns" :key="resultSign.code" :label="resultSign.name" :value="resultSign.code"></vxe-option>
                </vxe-select>
              </template>
            </vxe-column>
            <vxe-column title="自动审核" field="isAutoExamine" align="center" width="60">
              <template #default="{ row }">
                <vxe-checkbox v-model="row.isAutoExamine" :checked-value="1" :unchecked-value="0"></vxe-checkbox>
              </template>
            </vxe-column>
            <vxe-column title="是否危急值" field="isCritical" align="center" width="100">
              <template #default="{ row }">
                <vxe-checkbox v-model="row.isCritical" :checked-value="1" :unchecked-value="0"></vxe-checkbox>
              </template>
            </vxe-column>
            <vxe-column title="危急值显示字段" field="displayText" align="center" width="120" :edit-render="{ autofocus: '.vxe-input--inner' }">
              <template #default="{ row }">
                {{ row.displayText }}
              </template>
              <template #edit="{ row }">
                <vxe-input v-model="row.displayText" type="text"></vxe-input>
              </template>
            </vxe-column>
            <vxe-column title="判断方式" field="transtype" :edit-render="{}">
              <template #default="{ row }">
                <span>{{ format2(row.transtype) }}</span>
              </template>
              <template #edit="{ row }">
                <vxe-select v-model="row.transtype" placeholder="请选择处理方式" clearable transfer>
                  <vxe-option v-for="item in transtypes" :key="item.value" :label="item.label" :value="item.value"></vxe-option>
                </vxe-select>
              </template>
            </vxe-column>
            <vxe-column title="需复查标志" field="isReview" align="center" width="70">
              <template #default="{ row }">
                <vxe-checkbox v-model="row.isReview" :checked-value="1" :unchecked-value="0"></vxe-checkbox>
              </template>
            </vxe-column>
            <vxe-column title="是否在报告隐藏" field="isReview" align="center" width="70">
              <template #default="{ row }">
                <vxe-checkbox v-model="row.isHideRpt" :checked-value="1" :unchecked-value="0"></vxe-checkbox>
              </template>
            </vxe-column>
            <vxe-column title="快速输入码" field="quickInputCode" align="left" :edit-render="{ autofocus: '.vxe-input--inner' }">
              <template #edit="{ row }">
                <vxe-input v-model="row.quickInputCode" type="text"></vxe-input>
              </template>
            </vxe-column>
            <vxe-column title="是否大文本" field="isBigText" align="center" width="100">
              <template #default="{ row }">
                <vxe-checkbox v-model="row.isBigText" :checked-value="'1'" :unchecked-value="'0'"></vxe-checkbox>
              </template>
            </vxe-column>
            <!-- <vxe-column title="包含此文字即可" field="includeText" align="center" width="100">
              <template #default="{ row }">
                <vxe-checkbox v-model="row.includeText" :checked-value="1" :unchecked-value="0"></vxe-checkbox>
              </template>
            </vxe-column> -->
          </vxe-table>
        </div>
      </Pane>
    </Splitpanes>

    <vxe-modal v-model="bloodCulResultModal" width="35vw" height="35vh" show-footer :mask="false">
      <template #title>
        <span>文字结果</span>
      </template>
      <template #default>
        <div class="form-panel">
          <vxe-form :data="formData" title-width="80">
            <vxe-form-item title="" span="24">
              <template #default="{ data }">
                <vxe-textarea v-model="data.result" :autosize='{ minRows: 10 }'></vxe-textarea>
              </template>
            </vxe-form-item>

          </vxe-form>
        </div>
      </template>
      <template #footer>
        <vxe-button @click="bloodCulResultModal = false">关闭</vxe-button>
        <vxe-button type="text" class="button-blue margin-left-sm" @click="confirmTextResult" content="确定"></vxe-button>
      </template>
    </vxe-modal>
  </div>
</template>
<script lang="ts" setup>
// 引入组件区 ==========
import { reactive, ref, getCurrentInstance, onMounted, watch, inject } from 'vue';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { IsEmpty } from '@/utils/common';
import { VXETable, VxeFormPropTypes, VxeFormInstance, List } from 'vxe-table';
import { Splitpanes, Pane } from 'splitpanes';
import type { VxeTableInstance, VxeTablePropTypes, VxeTableEvents } from 'vxe-table';

// 全局变量定义区 ==========
// 获取屏幕高度
let conHeight = ref(document.documentElement.clientHeight);
let useSize = localStorage.getItem('uiSize');
let loading = ref(false); //模版加载

let { proxy }: any = getCurrentInstance();
let textResultList: any = reactive([]); //模版表格数据
let showDataLoading = ref(false);

// 当前选中项目
let itemDiceInfo: any = reactive({
  id: '',
  itemNo: '',
  rptGroupCode: '',
  itemDictCode: ''
});
// 结果标志 resultflag
let resultSigns: any = reactive([]); //字典表：指控规则类型

let iloading = ref(false);
const transResultRef = ref<VxeTableInstance<RowVO>>(); //表格ref标记

// 处理方式
const transtypes = [
  {
    value: '1',
    label: '相等'
  },
  {
    value: '2',
    label: '包含(%常用值%)'
  },
  {
    value: '3',
    label: '包含(常用值%)'
  },
  {
    value: '4',
    label: '包含(%常用值)'
  }
];

// 方法定义区 =============

//暴露属性和方法
defineExpose({
  initTextResult
});

// 页面加载时更新列表(左侧)
onMounted(() => {
  getDictDetailList();
});

//从词典获取所有的报告组
// 获取界面字段
function getDictDetailList() {
  iloading.value = true;
  resultSigns.length = 0;
  const url = '/api/sys/basDictDetail/findDictDetailList';
  proxy.$http
    .get(url, { params: { code: 'resultFlag' } })
    .then((res: any) => {
      resultSigns.push(...res.data);
      iloading.value = false;
    })
    .catch((err: any) => {
      iloading.value = false;
      proxy.$alert(err, '提示');
    });
}

function format1(value) {
  for (const item of resultSigns) {
    if (item.code == value) {
      return item.name;
    }
  }
}

function format2(value) {
  for (const item of transtypes) {
    if (item.value == value) {
      return item.label;
    }
  }
}

// 接收父组件传值
function initTextResult(row) {
  itemDiceInfo.id = row.id;
  itemDiceInfo.itemNo = row.itemNo;
  itemDiceInfo.rptGroup = row.rptGroup;
  if (!IsEmpty(row.rptGroup)) {
    findTextResult();
  }
}

function findTextResult() {
  loading.value = true;
  textResultList.length = 0;
  const url = '/lab/text/result/getTextResultByitemDictId';
  proxy.$http
    .get(url, { params: { itemNo: itemDiceInfo.itemNo, rptGroup: itemDiceInfo.rptGroup } })
    .then((res: any) => {
      textResultList.length = 0;
      textResultList.push(...res.data);
      loading.value = false;
    })
    .catch((err: any) => {
      loading.value = false;
      VxeMessage('查询列表失败', VxeNotifyType.error);
    });
}

//新增
function handleAdd() {
  const $table = proxy.$refs.transResultRef as any;
  if (IsEmpty(itemDiceInfo.rptGroup)) {
    VxeMessage('该项目的报告单元为空，请到基础信息完善。', VxeNotifyType.error);
    return;
  }
  if (IsEmpty(itemDiceInfo.itemNo)) {
    VxeMessage('该项目的项目代码为空，请到基础信息完善。', VxeNotifyType.error);
    return;
  }
  const newRec = {
    itemDictId: itemDiceInfo.id,
    itemNo: itemDiceInfo.itemNo,
    rptGroup: itemDiceInfo.rptGroup
  } as any;

  textResultList.push(newRec);
  $table.reloadData(textResultList);
  $table.setEditRow(newRec);
  proxy.$nextTick(() => {
    setTimeout(() => {
      $table.scrollToRow(newRec);
    }, 50);
  });
}

//删除
function handleDelete() {
  const arr = transResultRef.value?.getCheckboxRecords(true);
  // const sltTemplates = (proxy.$refs.templateTableRef as any).getCheckboxRecords();
  if (!(arr && arr.length > 0 && arr[0])) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        const url = '/lab/text/result/batchDeleteTextResult';
        proxy.$http
          .post(url, arr)
          .then((res: any) => {
            VxeMessage(res.status.msg, VxeNotifyType.success);
            findTextResult();
            // bacGermFormRef.value?.clearFormData();
          })
          .catch((err: any) => {
            VxeMessage(err, VxeNotifyType.error);
          });
      })
      .catch((err: any) => {
        VxeMessage(err, VxeNotifyType.error);
      });
  }
}
// 保存
function handleSave() {
  if (textResultList.length > 0) {
    const url = '/lab/text/result/saveOrUpdateTextResult';
    proxy.$http
      .post(url, textResultList)
      .then((res: any) => {
        VxeMessage('修改成功', VxeNotifyType.success);
        setTimeout(() => {
          findTextResult();
        }, 500);
      })
      .catch((err: any) => {
        VxeMessage('修改失败', VxeNotifyType.success);
      });
  } else {
    VxeMessage('无添加或修改数据', VxeNotifyType.error);
  }
}
async function pullTextResult() {
  showDataLoading.value = true;
  const url = '/api/sys/hisEmployee/getInterfaceData';
  const params = {
    url: (window as any).apiUrl.VUE_APP_INTERFACE_URL + '/labTextResult/getlabTextResult',
    code: 'GET_DICT_ITEM_TEXT_RESULT'
  };
  proxy.$http
    .get(url, { params: params })
    .then((res: any) => {
      VxeMessage('同步成功', VxeNotifyType.success);
      showDataLoading.value = false;
      findTextResult();
    })
    .catch((err: any) => {
      showDataLoading.value = false;
      VxeMessage('同步失败', VxeNotifyType.error);
    });
}
let bloodCulResultModal = ref(false); /**培养结果弹窗 */

let formData = reactive({
  result: ''
});

function dblclickresult(row: any) {
  if (IsEmpty(row)) {
    sltTextResult.value = {};
  } else {
    sltTextResult.value = row;
    formData.result = row.result;
    bloodCulResultModal.value = true;
  }
}
function confirmTextResult() {
  sltTextResult.value.result = formData.result;
  bloodCulResultModal.value = false;
}
let sltTextResult: any = ref({});
</script>
<style lang="scss" scoped>
#transResult {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  // 最右侧质控靶值设定表格
  .tableBox {
    height: calc(100% - 45px);
  }
}
</style>
